package com.taotao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.IDUtils;
import com.taotao.common.TaotaoResult;
import com.taotao.mapper.TbItemDescMapper;
import com.taotao.mapper.TbItemParamItemMapper;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemParamItem;
import com.taotao.service.TbItemService;
import com.taotao.common.EasyUIResult;
import com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class TbItemServiceImpl implements TbItemService {

    @Autowired
    private TbItemMapper tbItemMapper;

    @Autowired
    private TbItemDescMapper tbItemDescMapper;

    @Autowired
    private TbItemParamItemMapper tbItemParamItemMapper;

    /**
     * 查询商品
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public EasyUIResult getTbItemList(int pageNumber, int pageSize) {

        // 设置分页参数
        PageHelper.startPage(pageNumber, pageSize);
        List<TbItem> tbItemList = tbItemMapper.selectByExample(new TbItemExample());

        PageInfo<TbItem> pageInfo = new PageInfo<>(tbItemList);
        EasyUIResult easyUIResult = new EasyUIResult(pageInfo.getTotal(), tbItemList);
        return easyUIResult;
    }

    /**
     * 对商品进行删除、上下架操作
     * @param ids
     * @param status
     * @return
     */
    @Override
    @Transactional
    public TaotaoResult changeTbItemStatus(long[] ids, byte status) {
        int line = 0;
        for (int i = 0; i < ids.length; i++) {
            long id = ids[i];
            TbItem tbItem = new TbItem();
            tbItem.setId(id);
            tbItem.setStatus(status);
            line = tbItemMapper.updateByPrimaryKeySelective(tbItem) + line;
        }
        if (line == ids.length) {
            return TaotaoResult.build(200, "");
        }
        return null;
    }

    /**
     * 保存商品
     * @param cid
     * @param title
     * @param sellPoint
     * @param price
     * @param num
     * @param barcode
     * @param image
     * @param desc
     * @param itemParams
     * @return
     */
    @Override
    @Transactional
    public TaotaoResult addTbItem(long cid, String title, String sellPoint, long price, int num, String barcode, String image, String desc, String itemParams) {
        long itemId = IDUtils.genItemId();
        Date createdTime = new Date();
        Date updatedTime = new Date();

        // 保存商品
        TbItem tbItem = new TbItem();
        tbItem.setId(itemId);
        tbItem.setCid(cid);
        tbItem.setTitle(title);
        tbItem.setSellPoint(sellPoint);
        tbItem.setPrice(price);
        tbItem.setNum(num);
        tbItem.setBarcode(barcode);
        tbItem.setImage(image);
        tbItem.setStatus((byte) 1);
        tbItem.setCreated(createdTime);
        tbItem.setUpdated(updatedTime);
        tbItemMapper.insertSelective(tbItem);

        // 保存商品描述
        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setItemId(itemId);
        tbItemDesc.setItemDesc(desc);
        tbItemDesc.setCreated(createdTime);
        tbItemDesc.setUpdated(updatedTime);
        tbItemDescMapper.insertSelective(tbItemDesc);

        // 保存商品规格
        TbItemParamItem tbItemParamItem = new TbItemParamItem();
        tbItemParamItem.setItemId(itemId);
        tbItemParamItem.setParamData(itemParams);
        tbItemParamItem.setCreated(createdTime);
        tbItemParamItem.setUpdated(updatedTime);
        tbItemParamItemMapper.insertSelective(tbItemParamItem);

        return TaotaoResult.build(200, "");
    }

    /**
     * 对商品进行修改
     * @param id
     * @param cid
     * @param title
     * @param sellPoint
     * @param price
     * @param num
     * @param barcode
     * @param image
     * @param desc
     * @param itemParams
     * @param itemParamId
     * @return
     */
    @Override
    @Transactional
    public TaotaoResult updateTbItem(long id, long cid, String title, String sellPoint, long price, int num, String barcode, String image, String desc, String itemParams, long itemParamId) {
        Date updatedTime = new Date();

        // 修改商品
        TbItem tbItem = new TbItem();
        tbItem.setId(id);
        tbItem.setCid(cid);
        tbItem.setTitle(title);
        tbItem.setSellPoint(sellPoint);
        tbItem.setPrice(price);
        tbItem.setNum(num);
        tbItem.setBarcode(barcode);
        tbItem.setImage(image);
        tbItem.setUpdated(updatedTime);
        tbItemMapper.updateByPrimaryKeySelective(tbItem);

        // 修改商品描述
        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setItemId(id);
        tbItemDesc.setItemDesc(desc);
        tbItemDesc.setUpdated(updatedTime);
        tbItemDescMapper.updateByPrimaryKeySelective(tbItemDesc);

        // 修改商品规格
        TbItemParamItem tbItemParamItem = new TbItemParamItem();
        tbItemParamItem.setId(itemParamId);
        tbItemParamItem.setItemId(id);
        tbItemParamItem.setParamData(itemParams);
        tbItemParamItem.setUpdated(updatedTime);
        tbItemParamItemMapper.updateByPrimaryKeySelective(tbItemParamItem);

        return TaotaoResult.build(200, "");
    }
}
